package com.kinon.gfcanteen_v1.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kinon.gfcanteen_v1.R;
import com.kinon.gfcanteen_v1.adapter.OrderDetailAdapter;
import com.kinon.gfcanteen_v1.bean.BaseBean;
import com.kinon.gfcanteen_v1.bean.OrderDetailBean;
import com.kinon.gfcanteen_v1.customview.MyListView;
import com.kinon.gfcanteen_v1.network.CommonSubscriber;
import com.kinon.gfcanteen_v1.network.HttpManage;
import com.kinon.gfcanteen_v1.network.StrCallback;
import com.kinon.gfcanteen_v1.util.RxUtil;
import com.kinon.gfcanteen_v1.util.SPManager;
import com.kinon.gfcanteen_v1.view.base.BaseActivity;

import java.util.ArrayList;

import io.reactivex.disposables.Disposable;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by lhqq on 2018-01-08.
 */

public class OrderDetailActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_title;
    private ImageView img_back;
    private TextView tv_orderStatus;
    private TextView tv_orderTime;
    private TextView tv_orderUser;
    private TextView tv_reserveTime;
    private MyListView list_detail;
    private TextView tv_remarks;
    private Button btn_confirm;
    private int mId=0;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orderdetail_layout);
        initView();
    }

    private void initView() {
        tv_title= (TextView) findViewById(R.id.tv_title);
        img_back= (ImageView) findViewById(R.id.img_back);
        tv_orderStatus= (TextView) findViewById(R.id.tv_orderStatus);
        tv_orderTime= (TextView) findViewById(R.id.tv_orderTime);
        tv_orderUser= (TextView) findViewById(R.id.tv_orderUser);
        tv_reserveTime= (TextView) findViewById(R.id.tv_reserveTime);
        list_detail= (MyListView) findViewById(R.id.list_detail);
        tv_remarks= (TextView) findViewById(R.id.tv_remarks);
        btn_confirm= (Button) findViewById(R.id.btn_confirm);
        tv_title.setText("订单详情");

        String orderid= TextUtils.isEmpty(getIntent().getStringExtra("orderid"))?
                "":getIntent().getStringExtra("orderid");
        String orderon= TextUtils.isEmpty(getIntent().getStringExtra("orderon"))?
                "":getIntent().getStringExtra("orderon");
        img_back.setVisibility(View.VISIBLE);
        addListener();
        getDetail(orderid,orderon);
    }

    private void addListener() {
        img_back.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);
    }

    private void getDetail(String orderid,String orderno){
        Call<String> call=HttpManage.getInstance().getDetails
                (SPManager.getInstance().getToken(),orderid,orderno);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                Gson gson=new Gson();
                OrderDetailBean detail=gson.fromJson(data,OrderDetailBean.class);
                showView(detail);
            }

            @Override
            public void onError(int code, String error) {
             mToast(error);
            }
        });
    }

    private void showView(OrderDetailBean bean){
        mId=bean.getId();
        if(bean.getState()==1){
          tv_orderStatus.setText("未处理");
        }else{
          tv_orderStatus.setText("已处理");
            btn_confirm.setVisibility(View.GONE);
        }
        tv_orderTime.setText(bean.getOrder_time());
        tv_orderUser.setText(bean.getName());
        tv_reserveTime.setText(bean.getBook_time());
        OrderDetailAdapter adapter=new
                OrderDetailAdapter(bean.getDetails(),OrderDetailActivity.this);
        list_detail.setAdapter(adapter);
        tv_remarks.setText(bean.getNote());

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_back:
                finish();
                break;
            case R.id.btn_confirm:
                if(mId!=0){
                    updateOrder();
                }
                break;
        }
    }

    private void updateOrder(){
        Call<String> call=HttpManage.getInstance()
                .updateOrder(SPManager.getInstance().getToken(),mId,2);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                mToast("取餐成功");
                finish();
            }

            @Override
            public void onError(int code, String error) {
                mToast(error);
            }
        });
    }
}
